package com.tianji.learning.mq;

import com.tianji.common.constants.MqConstants;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.mq.message.SignInMessage;
import com.tianji.learning.service.IPointsRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

//积分功能-消费者监听器
@Slf4j
@Component
@RequiredArgsConstructor
public class LearningPointsListener {
    private final IPointsRecordService recordService;

    //监听-新增互动问答事件
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "qa.points.queue", durable = "true"),
            exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.WRITE_REPLY   //写回答reply.new
    ))
    public void listenWriteReplyMessage(Long userId){
        log.info("积分功能-监听到新增互动问答事件:{}",userId);
        //写问答 积分+5 ，每日上限20分
        recordService.addPointsRecord(userId,5, PointsRecordType.QA);
    }



    //监听-签到事件
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "sign.points.queue", durable = "true"),
            exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.WRITE_REPLY
    ))
    public void listenWriteReplyMessage(SignInMessage signInMessage){
        log.info("积分功能-监听到签到事件:{}",signInMessage);
        //没有上限，每次1分，连续7天奖励10分  连续14天 奖励20  连续28天奖励40分
        recordService.addPointsRecord(signInMessage.getUserId(),signInMessage.getPoints(),PointsRecordType.SIGN);
    }



    //监听-完成一小节事件
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "learning.points.queue", durable = "true"),
            exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.LEARN_SECTION
    ))
    public void listenLearnSectionMessage(Long userId){
        log.info("积分功能-监听到完成一小节事件:{}",userId);
        //每学习一小节，积分+10，每天上限50分
        recordService.addPointsRecord(userId,10, PointsRecordType.LEARNING);
    }


    //监听-写笔记事件
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "note.new.points.queue", durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.WRITE_NOTE
    ))
    public void listenWriteNodeMessage(Long userId){
        log.info("监听到写笔记事件:{}",userId);
        //写笔记 积分+3，每日上限20分
        recordService.addPointsRecord(userId, 3, PointsRecordType.NOTE);
    }



    //监听-笔记被采集事件
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "note.gathered.points.queue", durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.NOTE_GATHERED
    ))
    public void listenNodeGatheredMessage(Long userId){
        log.info("监听到笔记被采集事件:{}",userId);
        //笔记每次被采集+2分 每日上限20分
        recordService.addPointsRecord(userId, 2, PointsRecordType.NOTE);
    }
}
